package Controller;

import Util.JDBCUtill;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import pojo.classroom;

import java.sql.SQLException;
import java.util.List;


public class ClassRoomController {

    @FXML
    private TableView<classroom> ClassroomTablel;
    @FXML
    private TableColumn<classroom, String> CapacityFile;
    @FXML
    private TableColumn<classroom, String> CLassIDFile;
    @FXML
    private TableColumn<classroom, String> StartTimeFile;
    @FXML
    private TableColumn<classroom, String> EndTimeFile;

    @FXML
    private TableColumn<classroom, String>EquipmentFile;

    @FXML
    private Button addbut;

    @FXML
    private Button changebut;

    @FXML
    private Button deletebut;
    @FXML
    private TextField ClasscheckField;
    @FXML
    private Button teacherbut;
    @FXML
    private Button userbut;
    @FXML
    private TableColumn<classroom, String> Available;

    @FXML
    private Button CheckBut;
    @FXML
    void initialize() {
        System.out.println(Main.SharedData.isAdmin);
        if(Main.SharedData.isAdmin==1) {
            teacherbut.setVisible(true);
            userbut.setVisible(true);
            addbut.setVisible(true);
            changebut.setVisible(true);
            deletebut.setVisible(true);
        }
        getDates();
    }
    private void getDates() {
        List<classroom> classrooms = JDBCUtill.readAllDate();
        CLassIDFile.setCellValueFactory(new PropertyValueFactory<>("CId"));
        CapacityFile.setCellValueFactory(new PropertyValueFactory<>("Capacity"));
        EquipmentFile.setCellValueFactory(new PropertyValueFactory<>("Equipment"));
        Available.setCellValueFactory(new PropertyValueFactory<>("Available"));
        StartTimeFile.setCellValueFactory(new PropertyValueFactory<>("StartTime"));
        EndTimeFile.setCellValueFactory(new PropertyValueFactory<>("EndTime"));
        ClassroomTablel.setItems(FXCollections.observableArrayList(classrooms));
    }
    public void showAddView(){
        Main.addView("ClassroomAdd-view.fxml");
    }
    @FXML
    public void doDelete() throws SQLException {
        classroom classroom = ClassroomTablel.getSelectionModel().getSelectedItem();
        if (classroom != null) {
            JDBCUtill.delete(classroom.getCId());
            getDates();
        }
    }
    @FXML
    public void CheckAction() {
        String classid = ClasscheckField.getText();
        List<classroom> classrooms = JDBCUtill.checkDate(classid);
        CLassIDFile.setCellValueFactory(new PropertyValueFactory<>("CId"));
        CapacityFile.setCellValueFactory(new PropertyValueFactory<>("Capacity"));
        EquipmentFile.setCellValueFactory(new PropertyValueFactory<>("Equipment"));
        Available.setCellValueFactory(new PropertyValueFactory<>("Available"));
        StartTimeFile.setCellValueFactory(new PropertyValueFactory<>("StartTime"));
        EndTimeFile.setCellValueFactory(new PropertyValueFactory<>("EndTime"));
        ClassroomTablel.setItems(FXCollections.observableArrayList(classrooms));
    }
    @FXML
    public void showUpdateView(){
        classroom classroom = ClassroomTablel.getSelectionModel().getSelectedItem();
        if (classroom != null) {
            ClassroomUpdateController.setCurrent(classroom);
            Main.addView("ClassroomUpdate-view.fxml");
        }
    }
    public void reflush() {
        getDates();
    }
    @FXML
    void ClassInternationControl(ActionEvent event) {
     initialize();
    }

    @FXML
    void TeacherInternationControl(ActionEvent event) {
        Main.changeView("teacher-view.fxml");
    }


    @FXML
    void UserInternationControl(ActionEvent event) {
        Main.changeView("user-view.fxml");
    }
}
